﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Core;
using GSLMS.Entity.Enum;
using GSLMS.Entity;

namespace GSLMS.WebAPP.Areas.DianDang.Controllers.Apply.RefRegInfo
{
    public class RefRegInfoController : AdminBaseController
    {
        protected override string navTabId
        {
            get { return "DianDang_Apply_RefRegInfo"; }
        }
        //
        // GET: /DianDang/Special/

        public ActionResult Index(FormCollection form)
        {
            var searchParma = new TB_APP_REFREGINFO();
            SearchPara<TB_APP_REFREGINFO>(form, searchParma);

            searchParma.STATUS = Request["STATUS"] == null ? -1 : Helper.ToInt(Request["STATUS"]);
            searchParma.StrSort = " id desc";
            var specailList = serviceRefRegInfo.GetList(searchParma) ?? new List<TB_APP_REFREGINFO>();
            ViewData["RowCount"] = searchParma.RowCount;
            return View(specailList);
        }

        //
        // GET: /DianDang/Special/Details/5

        public ActionResult Details(int id)
        {
            var model = serviceRefRegInfo.GetByID(id);
            return View(model);
        }

        //
        // GET: /DianDang/Special/Create

        public ActionResult Create(int? id)
        {
            var model = new TB_APP_REFREGINFO()
            {
                REFBY = CurentUser.UserName,
                OUTTIME = DateTime.Now
            };
            if (id.HasValue && id > 0)
            {
                model = serviceRefRegInfo.GetByID(id.Value) ?? new TB_APP_REFREGINFO();
            }
            var searchParm = new TB_APP_REGINFO()
            {
                SubAssignedIn = UniversallySearchParma.SubAssignedIn,
                Pagination = new Entity.DTO.PaginationDTO() { IsPaging = false }
            };

            searchParm.Pagination.PageSize = -1;

            ViewData["CustList"] = new SelectList(serviceRegInfo.GetList(searchParm), "ID", "CUSTNAME");
            return View(model);
        }

        [HttpPost]
        public JsonResult GetRefCustomerInfo(int? id)
        {
            var custModel = new TB_APP_REGINFO();
            if (id.HasValue)
            {
                custModel = serviceRegInfo.GetByID(id.Value) ?? new TB_APP_REGINFO();
            }
            return Json(custModel);
        }

        //
        // POST: /DianDang/Special/Create

        [HttpPost]
        public string Create(FormCollection form)
        {
            string result = string.Empty;
            TB_APP_REFREGINFO model = new TB_APP_REFREGINFO();
            TryUpdateModel(model, form);

            try
            {
                bool isSumbit = model.ID > 0 ? serviceRefRegInfo.Update(model) : serviceRefRegInfo.Add(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }


        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /DianDang/Special/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
